#include <Windows.h>
#include <conio.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <mmsystem.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <thread>
#include <vector>
#include <windows.h>
// #include "roles.cpp"
#include "Simulated_universe.h"
#include "atk_SU.h"
#include "attack.h"
#include "func.h"
#include "information1.h"
#include "main.h"

#include "music.h"

using namespace std;
extern int achievement1[100], easter_egg[100], opt[100], si_baodi, wu_baodi,
    arms_[100], form_yx, form_yy;
;
// extern int map_U[1100][1100];
extern int map_U[1100][1100];
extern int mapsize_U, pdchangerole;

struct incident {
    int x, y;
    int id;
} Incidents[100];

//

int inc_number = 0;
char path0[100] = "Universe_map\\\\map_", add_[10] = ".txt", path[100];
// sprintf(str3,"%s%s",str1,str2);
extern int map_x, map_y;
// void COLOR_PRINT1(const char* s, int front_color,int back_color);

void Read_map(int position) {
    sprintf(path, "%s%c%s", path0, position + '0', add_);
    cout << path;
    ifstream infile;
    infile.open(path, ios::in);
    // if (!infile.is_open()){
    // 	cout << "读取文件失败" << endl;
    // }
    int line1 = 0;
    char tmp_map_U[1100];
    infile >> map_x >> map_y >> you.x >> you.y >> inc_number;
    for (int i = 1; i <= inc_number; i++)
        infile >> Incidents[i].x >> Incidents[i].y;
    for (int i = 1; i <= map_x; i++) {
        infile >> tmp_map_U;
        for (int j = 0; j < map_y; j++) {
            if (tmp_map_U[j] >= '0' && tmp_map_U[j] <= '9')
                map_U[i + 100][j + 100] = int(tmp_map_U[j]) - 48;
            else
                map_U[i + 100][j + 100] = int(tmp_map_U[j]);
            // cout<<map_U[i+100][j+100];
        }
        // cout<<endl;
    }
    infile.close();
}

void Interface() {
    system("cls");
    // printf("模拟宇宙");
    Read_map(1);
    print(map_x, map_y, 2);
    you.x += 100;
    you.y += 100;
    int yx = you.x, yy = you.y;
    char keyin;
    // yx=you.x-15;yy=you.y-15;
    // if(you.x<15)yx=1;
    // if(you.y<15)yy=1;
    // cout<<you.x<<" "<<you.y<<" "<<yx<<" "<<yy;
    while (1) {
        // while(!_kbhit());
        keyin = getch();
        if (keyin >= '1' && keyin <= '6') {
            switch (keyin) {
            case '1':
                you.role = "trailblazer";
                break;
            case '2':
                // if (you.role_status[2].available == 1)
                you.role = "Raiden Mei";
                break;
            case '3':
                if (you.role_status[3].available == 1)
                    you.role = "Firefly";
                break;
            default:
                break;
            }
            cout << keyin << you.role_status[2].available << you.role << endl;
        }
        if (keyin == 'y') {
            system("cls");
            yue_qian();
        }
        if (keyin == '=') {
            printf("你要退出模拟宇宙吗? y/n :[");
            char tc = getch();
            if (tc == 'y')
                return;
            else
                system("cls");
        }
        if (keyin == 'c') {
            // system("cls");
            Role_broad();
        }
        if (keyin == 'q') {
            Q_Su(yx, yy, 2);
            system("cls");
        }
        if (keyin == 'h') {
            help();
        }
        if (keyin == 'g') {
            achievements();
        }
        if (keyin == 'o') {
            options(opt);
        }
        if (keyin == 'f') {
            int axx = you.x, ayy = you.y;
            if (you.face == 1) {
                axx--;
            }
            if (you.face == 2) {
                ayy++;
            }
            if (you.face == 3) {
                axx++;
            }
            if (you.face == 4) {
                ayy--;
            }
            char temp = map_U[axx][ayy];
            map_U[axx][ayy] = '+';
            _Clearscreen(0);
            print(map_x, map_y, 2);
            if (temp == '!') {
                information2(axx, ayy, easter_egg, opt[1]);
                // system("pause");
                // print(yx,yy,1);
            }
            Sleep(50);
            map_U[axx][ayy] = temp;
            _Clearscreen(0);
            print(map_x, map_y, 2);
        }
        if (keyin == 'w') {
            if (you.x > 101) {
                if (map_U[you.x - 1][you.y] == 0) {
                    you.x--;
                }
                you.face = 1;
            }
        } else if (keyin == 'a') {
            if (you.y > 101) {
                if (map_U[you.x][you.y - 1] == 0)
                    you.y--;
            }
            you.face = 4;
        } else if (keyin == 's') {
            if (you.x < map_x + 101) {
                if (map_U[you.x + 1][you.y] == 0)
                    you.x++;
            }
            you.face = 3;
        } else if (keyin == 'd') {
            if (you.y < map_y + 101) {
                if (map_U[you.x][you.y + 1] == 0)
                    you.y++;
            }
            you.face = 2;
        }
        if (keyin == 'j') {
            attack_Su(yx, yy, 2);
        } else if (keyin == 'k') {
            if (you.face == 1) {
                you.x -= 2;
            }
            if (you.face == 2) {
                you.y += 2;
            }
            if (you.face == 3) {
                you.x += 2;
            }
            if (you.face == 4) {
                you.y -= 2;
            }
        }
        // system("cls");
        _Clearscreen(0);
        print(map_x, map_y, 2);
    }
    system("pause");
    system("cls");
}

// int main(){
//     you.x=4;you.y=102;
//     Read_map();
//     print_U();
//     return 0;
// }

// void print_U() {
//     printf("\n");
//     for (int a = 1; a <= 51; a++)
//         printf("--");
//     printf("\n");
//     for (int i = 101; i <= map_y + 100; i++) {
//         printf("|");
//         for (int j = 101; j <= map_x + 100; j++) {
//             // cout<<map_U[i][j];
//             if (i == you.x && j == you.y) {
//                 printf("@ ");
//             } else {
//                 if (map_U[i][j] == 0)
//                     printf("%c ", ' ');
//                 else if (map_U[i][j] == 1)
//                     printf("%c ", 'X');
//                 else if (map_U[i][j] == 119) {
//                     COLOR_PRINT1("O", 8, 4);
//                     // printf("O ");
//                     printf(" ");
//                 } else if (map_U[i][j] == 210) {
//                     COLOR_PRINT1("//", 8, 4);
//                     printf("");
//                 } else if (map_U[i][j] == 211) {
//                     COLOR_PRINT1("\\\\", 8, 4);
//                     printf("");
//                 } else if (map_U[i][j] == 212) {
//                     COLOR_PRINT1("--", 8, 4);
//                     // printf(" ");
//                 } else if (map_U[i][j] == 213) {
//                     COLOR_PRINT1("--", 4, 4);
//                     // printf(" ");
//                 } else
//                     printf("%c ", (char)map_U[i][j]);
//             }
//         }
//         printf("|");
//         if (i == 101)
//             printf("UID:1145141919810");
//         if (i == 102) {
//             printf("1: 开拓者Trailblazer");
//             if (you.role == "trailblazer") {
//                 printf(" * ");
//                 // pdchangerole=0;
//             } else {
//                 printf("   ");
//             }
//         }
//         if (i == 103 && you.role_status[2].available == 1) {
//             printf("2: 黄泉Raiden Mei");
//             if (you.role == "Raiden Mei") {
//                 printf("    * ");
//             } else {
//                 printf("       ");
//             }
//         }
//         if (i == 104 && you.role_status[3].available == 1) {
//             printf("3: 流萤Firefly");
//             if (you.role == "Firefly") {
//                 printf("      * ");
//             } else {
//                 printf("         ");
//             }
//         }
//         if (i == 111) {
//             printf("点击h获得帮助(走不动路可能是因为大写，调成小写就可以了)");
//         }
//         printf("\n");
//     }
//     // printf("   ");
//     for (int a = 1; a <= 51; a++)
//         printf("--");
// }

// void COLOR_PRINT1(const char* s, int front_color,int back_color)
// {
//  HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
//  SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | back_color*16
//  |FOREGROUND_INTENSITY | front_color); printf(s);
//  SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
// }